{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
    "def CI_W2(z1,z2, pk1, pk2, xkk, pkk, i):  # 融合数据函数\n",
    "    '''\n",
    "    z1: 数据1\n",
    "    z2: 数据2\n",
    "    pk1: rmse1\n",
    "    pk2: rmse2\n",
    "    xkk：\n",
    "    pkk：\n",
    "    i: \n",
    "    '''\n",
    "    args = tuple(np.concatenate((pk1, pk2),axis=0)) #转为元组 按照axis轴连接array组成新的arrau\n",
    "    def f(x, *args)\n",
    "        pk1, pk2 = args\n",
    "        return 1/(x[0] * (1 / pk1) + x[1] * (1 / pk2))\n",
    "    def gradf(x, *args):\n",
    "        pk1, pk2 = args\n",
    "        gw1 = (-1)/ (pk1 *(x[0] * (1 / pk1) + x[1] * (1 / pk2) )**2)\n",
    "        gw2 = (-1)/ (pk2 *(x[0] * (1 / pk1) + x[1] * (1 / pk2) )**2)\n",
    "        return np.array([gw1, gw2])\n",
    "    ##约束条件\n",
    "\n",
    "    cons = ({'type': 'eq',  # 等式约束条件\n",
    "             'fun': lambda x: np.array([x[0]+x[1]-1]),  # 方程\n",
    "             'jac': lambda x: np.array([1,1])\n",
    "             },  # 导数\n",
    "            {'type': 'ineq',  # 不等式约束条件\n",
    "             'fun': lambda x: np.array([x[0],x[1],1-x[0],1-x[1]]),   # 方程,[x[1] - 1],[x[2] - 1]\n",
    "             'jac': lambda x: np.array([[1,0],[0,1],[-1,0],[0,-1]]),\n",
    "             }#,[1, 1.0],[1, 1.0]]\n",
    "            )  # 导数\n",
    "    res = minimize(f, [0.5,0.5], args=args, jac=gradf,\n",
    "                   constraints=cons, method='SLSQP', options={'disp': True},tol=0.00001)\n",
    "    #L-BFGS-B,TNC\n",
    "    print(res.x)\n",
    "    w1 = res.x[0]\n",
    "    w2 = res.x[1]    \n",
    "    pkk = 1 / ( w1*(1/pk1) + w2 *(1/pk2))  # 求得最小协方差pkk\n",
    "    pk_rmse = []\n",
    "    pk_rmse.append(pkk)\n",
    "    # print(pk_rmse)\n",
    "    xf = []\n",
    "    for j in range(i * 24, (i + 1) * 24):\n",
    "        xkk = pkk * ( w1*1/pk1*z1[j] + w2*1/pk2 * z2[j])  # 根据最小协方差及权重wl进行预测  a、b两组同一时刻的数据同时进\n",
    "        # print(xkk)\n",
    "        xf.append(xkk)\n",
    "    return xf,pk_rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def CI_W3(z1,z2,z3, pk1,pk2,pk3, xkk, pkk, i):  # 融合数据函数\n",
    "    args = tuple(np.concatenate((pk1,pk2,pk3),axis=0))\n",
    "    def f(x, *args):\n",
    "        pk1,pk2,pk3 = args\n",
    "        return 1/(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3) )\n",
    "    def gradf(x, *args):\n",
    "        pk1,pk2,pk3 = args\n",
    "        gw1 = (-1)/ (pk1 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3) )**2)\n",
    "        gw2 = (-1)/ (pk2 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3) )**2)\n",
    "        gw3 = (-1)/ (pk3 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3) )**2)\n",
    "        return np.array([gw1,gw2,gw3])\n",
    "    ##约束条件\n",
    "\n",
    "    cons = ({'type': 'eq',  # 等式约束条件\n",
    "             'fun': lambda x: np.array([x[0]+x[1]+x[2]-1]),  # 方程\n",
    "             'jac': lambda x: np.array([1,1,1])\n",
    "             },  # 导数\n",
    "            {'type': 'ineq',  # 不等式约束条件\n",
    "             'fun': lambda x: np.array([x[0],x[1],x[2],1-x[0],1-x[1],1-x[2]]),   # 方程,[x[1] - 1],[x[2] - 1]\n",
    "             'jac': lambda x: np.array([[1,0,0],[0,1,0],[0,0,1],[-1,0,0],[0,-1,0],[0,0,-1]]),\n",
    "             }#,[1, 1.0],[1, 1.0]]\n",
    "            )  # 导数\n",
    "    res = minimize(f, [0.5,0.2,0.3], args=args, jac=gradf,\n",
    "                   constraints=cons, method='SLSQP', options={'disp': True},tol=0.00001)\n",
    "    #L-BFGS-B,TNC\n",
    "    print(res.x)\n",
    "    w1 = res.x[0]\n",
    "    w2 = res.x[1]   \n",
    "    w3 = res.x[2]\n",
    "    pkk = 1 / ( w1*(1/pk1) + w2 *(1/pk2) + w3 *(1/pk3))  # 求得最小协方差pkk\n",
    "    pk_rmse = []\n",
    "    pk_rmse.append(pkk)\n",
    "    # print(pk_rmse)\n",
    "    xf = []\n",
    "    for j in range(i * 24, (i + 1) * 24):\n",
    "        xkk = pkk * ( w1*1/pk1*z1[j] + w2*1/pk2 * z2[j]+ w3*1/pk3 * z3[j])  # 根据最小协方差及权重wl进行预测  a、b两组同一时刻的数据同时进\n",
    "        # print(xkk)\n",
    "        xf.append(xkk)\n",
    "    return xf,pk_rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def CI_W4(z1,z2,z3,z4, pk1,pk2,pk3,pk4, xkk, pkk, i):  # 融合数据函数\n",
    "    args = tuple(np.concatenate((pk1,pk2,pk3,pk4),axis=0))\n",
    "    def f(x, *args):\n",
    "        pk1,pk2,pk3,pk4 = args\n",
    "        return 1/(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3) + x[3] * (1 / pk4))\n",
    "    def gradf(x, *args):\n",
    "        pk1,pk2,pk3,pk4 = args\n",
    "        gw1 = (-1)/ (pk1 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3)+ x[3] * (1 / pk4) )**2)\n",
    "        gw2 = (-1)/ (pk2 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3)+ x[3] * (1 / pk4) )**2)\n",
    "        gw3 = (-1)/ (pk3 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3)+ x[3] * (1 / pk4) )**2)\n",
    "        gw4 = (-1)/ (pk4 *(x[0] * (1 / pk1) + x[1] * (1 / pk2)+ x[2] * (1 / pk3)+ x[3] * (1 / pk4) )**2)\n",
    "        return np.array([gw1,gw2,gw3,gw4])\n",
    "    ##约束条件\n",
    "\n",
    "    cons = ({'type': 'eq',  # 等式约束条件\n",
    "             'fun': lambda x: np.array([x[0]+x[1]+x[2]+x[3]-1]),  # 方程\n",
    "             'jac': lambda x: np.array([1,1,1,1])\n",
    "             },  # 导数\n",
    "            {'type': 'ineq',  # 不等式约束条件\n",
    "             'fun': lambda x: np.array([x[0],x[1],x[2],x[3],1-x[0],1-x[1],1-x[2],1-x[3] ]),   # 方程,[x[1] - 1],[x[2] - 1]\n",
    "             'jac': lambda x: np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1],[-1,0,0,0],[0,-1,0,0],[0,0,-1,0],[0,0,0,-1]]),\n",
    "             }#,[1, 1.0],[1, 1.0]]\n",
    "            )  # 导数\n",
    "    res = minimize(f, [0.3,0.2,0.2,0.3], args=args, jac=gradf,\n",
    "                   constraints=cons, method='SLSQP', options={'disp': True},tol=0.00001)\n",
    "    #L-BFGS-B,TNC\n",
    "    print(res.x)\n",
    "    w1 = res.x[0]\n",
    "    w2 = res.x[1]   \n",
    "    w3 = res.x[2]\n",
    "    w4 = res.x[3]\n",
    "    pkk = 1 / ( w1*(1/pk1) + w2 *(1/pk2) + w3 *(1/pk3) + w4 *(1/pk4))  # 求得最小协方差pkk\n",
    "    pk_rmse = []\n",
    "    pk_rmse.append(pkk)\n",
    "    # print(pk_rmse)\n",
    "    xf = []\n",
    "    for j in range(i * 24, (i + 1) * 24):\n",
    "        xkk = pkk * ( w1*1/pk1*z1[j] + w2*1/pk2 * z2[j]+ w3*1/pk3 * z3[j] + w4*1/pk4 * z4[j])  # 根据最小协方差及权重wl进行预测  a、b两组同一时刻的数据同时进\n",
    "        # print(xkk)\n",
    "        xf.append(xkk)\n",
    "    return xf,pk_rmse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
